//
//  ConBaseIntroVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/2/6.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "ConBaseIntroVc.h"
#import "ClubTextImageCell.h"
#import "CommendImageTextModel.h"
#import <SDWebImageManager.h>
#import "ImageTitleModel.h"

@interface ConBaseIntroVc ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation ConBaseIntroVc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.clubDetailList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImageTitleModel *textModel = self.model.clubDetailList[indexPath.row];
    if (textModel.cellH == 0 || !textModel.isFinshDownLoad || !textModel.isCalculateAga) {//没有下载完就来计算的话 model里面有高度值 再进来就不计算了,就算标记下载完了 还没有去重新算一遍
        ClubTextImageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ClubTextImageCell class])];
        textModel.cellH = [cell calculateDetailCellHWithModel:textModel];
        if (textModel.isFinshDownLoad) {
            textModel.isCalculateAga = YES;
        }
    }
    return textModel.cellH;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClubTextImageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ClubTextImageCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.model.clubDetailList[indexPath.row];
    return cell;
}
- (void)setModel:(ClubModel *)model
{
    _model = model;
    [self.tableView reloadData];
    [self calculateImagesHeight:self.model.clubDetailList];
}
//缓存图片计算图片高度
- (void)calculateImagesHeight:(NSArray *)arr
{
    __WEAK
    //    [LottieLoading showLoadingInView:self];
    dispatch_group_t group = dispatch_group_create();
    for (ImageTitleModel *model in arr) {
        dispatch_group_enter(group);
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:model.clubImage] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            if (finished) {
                model.isFinshDownLoad = YES;
                [weakSelf.tableView reloadData];//下载完就计算一次
                dispatch_group_leave(group);
            }
        }];
    }
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        //        [LottieLoading hideLoadingInView:self];//下载完图片在隐藏指示器
        //        [weakSelf.tableView reloadData];
    });
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-self.roolH-32)];
        [_tableView registerClass:[ClubTextImageCell class] forCellReuseIdentifier:NSStringFromClass([ClubTextImageCell class])];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}

@end
